angular.module('app.baseinfo')
    .controller('locationCtrl', ['$scope', '$filter', '$modal', 'toaster', 'locationService',
        function ($scope, $filter, $modal, toaster, locationService) {
            $scope.query = "";
            $scope.location = {};
            $scope.pager = {
                pageNo: 1,
                pageSize: 10,
                totalItems: 0
            };
           /* $scope.pager1 = {
                pageNo: 1,
                pageSize: 10,
                totalItems: 0
            };*/
            $scope.getLocation = function () {
                $scope.locations=[];
                var query = {
                    pageNo: $scope.pager.pageNo,
                    pageSize: $scope.pager.pageSize
                };
                if (query) {
                    query.location = $scope.query || "";
                    query.areaCode = $scope.location.areaCode.code;
                    if ($scope.areaCodes[0] == $scope.location.areaCode) {
                        query.areaCode = "";
                    }
                }
                locationService.getLocation(query).then(function (res) {
                    $scope.pager.totalItems = res.data.totalItems;
                    $scope.locations = res.data.data;
                }, function (res) {
                    toaster.pop("error", "错误", res.errMsg || "查询失败")
                });
            };


            $scope.getAreaCodes=function(){
                var query = {};
                locationService.getAreas(query).then(function (res) {
                    $scope.areaCodes=[{"name":"全部"}];
                    $scope.areaCodes= $scope.areaCodes.concat(res.data.data);
                    $scope.location.areaCode= $scope.areaCodes[0];
                    $scope.getLocation();
                }, function (res) {
                    toaster.pop("error", "错误", res.errMsg || "查询失败")
                });
            };
            $scope.getAreaCodes();

            $scope.getAreas=function(){
                $scope.areas=[];
               var query = {
                   /* pageNo: $scope.pager1.pageNo,
                    pageSize: $scope.pager1.pageSize*/
                };
                if (query) {
                    query.query = $scope.search || "";
                }
                locationService.getAreas(query).then(function (res) {
                   /* $scope.pager1.totalItems = res.data.totalItems;*/
                    $scope.areas = res.data.data;
                }, function (res) {
                    toaster.pop("error", "错误", res.errMsg || "查询失败")
                });
            };
            $scope.getAreas();

            $scope.searchLocation = function () {
                $scope.pager = {
                    pageNo: 1,
                    pageSize: 10,
                    totalItems: 0
                };
                $scope.getLocation();
            };

            $scope.searchArea = function () {
               /* $scope.pager1 = {
                    pageNo: 1,
                    pageSize: 10,
                    totalItems: 0
                };*/
                $scope.getAreas();
            };
            $scope.addLocation = function () {
                var modalInstance = $modal.open({
                    animation: true,
                    templateUrl: 'partials/baseinfo/location-modal.html',
                    controller: 'addLocationCtrl',
                    size: 'md',
                    resolve: {
                        location: function () {
                            return null;
                        }
                    }
                });
                modalInstance.result.then(function () {
                    $scope.pager = {
                        pageNo: 1,
                        pageSize: 10,
                        totalItems: 0
                    };
                    $scope.getLocation();
                });
            };

            $scope.addArea = function () {
                var modalInstance = $modal.open({
                    animation: true,
                    templateUrl: 'partials/baseinfo/area-modal.html',
                    controller: 'addAreaCtrl',
                    size: 'md',
                    resolve: {
                        area: function () {
                            return null;
                        }
                    }
                });
                modalInstance.result.then(function () {
                   /* $scope.pager1 = {
                        pageNo: 1,
                        pageSize: 10,
                        totalItems: 0
                    };*/
                    $scope.getAreas();
                    $scope.getAreaCodes();
                });
            };

            $scope.deleteLocation = function (location) {
                var modalInstance = $modal.open({
                    animation: true,
                    templateUrl: 'partials/confirm/confirm.html',
                    controller: 'confirmCtrl',
                    size: 'sm',
                    resolve: {
                        content: function () {
                            return "确认删除？";
                        }
                    }
                });
                modalInstance.result.then(function () {
                    locationService.deleteLocation(location).then(function (res) {
                        toaster.pop("success", "成功", "删除库位成功");
                        $scope.pager = {
                            pageNo: 1,
                            pageSize: 10,
                            totalItems: 0
                        };
                        $scope.getLocation();
                    }, function (res) {
                        toaster.pop("error", "错误", res.errMsg || "删除库位失败");
                    });
                });
            };
            $scope.deleteArea = function (area) {
                var modalInstance = $modal.open({
                    animation: true,
                    templateUrl: 'partials/confirm/confirm.html',
                    controller: 'confirmCtrl',
                    size: 'sm',
                    resolve: {
                        content: function () {
                            return "确认删除？";
                        }
                    }
                });
                modalInstance.result.then(function () {
                    locationService.deleteArea(area).then(function (res) {
                        toaster.pop("success", "成功", "删除区域成功");
                       /* $scope.pager1 = {
                            pageNo: 1,
                            pageSize: 10,
                            totalItems: 0
                        };*/
                        $scope.getAreas();
                        $scope.getAreaCodes();
                    }, function (res) {
                        toaster.pop("error", "错误", res.errMsg || "删除区域失败");
                    });
                });
            }
        }])
    .controller('addLocationCtrl', ['$scope', '$filter', '$modal', '$modalInstance', 'toaster', 'locationService',
        function ($scope, $filter, $modal, $modalInstance, toaster, locationService) {
            $scope.location = {};
            $scope.getAreaCodes=function(){
                var query = {};
                locationService.getAreas(query).then(function (res) {
                    $scope.location.areaCodes = res.data.data;
                }, function (res) {
                    toaster.pop("error", "错误", res.errMsg || "查询失败")
                });
            };
            $scope.getAreaCodes();
            $scope.ok = function () {
                $scope.location.areaCode= $scope.location.areaCode.code;
                locationService.addLocation($scope.location).then(function (res) {
                    toaster.pop("success", "成功", "新增库位成功");
                    $modalInstance.close();
                }, function (res) {
                    toaster.pop("error", "错误", res.errMsg || "新增库位失败");
                });
            };
            $scope.cancel = function () {
                $modalInstance.dismiss('cancel');
            };
        }])
    .controller('addAreaCtrl', ['$scope', '$filter', '$modal', '$modalInstance', 'toaster', 'locationService',
        function ($scope, $filter, $modal, $modalInstance, toaster, locationService) {
            $scope.area = {};

            $scope.ok = function () {
                locationService.addArea($scope.area).then(function (res) {
                    toaster.pop("success", "成功", "新增区域成功");
                    $modalInstance.close();
                }, function (res) {
                    toaster.pop("error", "错误", res.errMsg || "新增区域失败");
                });
            };
            $scope.cancel = function () {
                $modalInstance.dismiss('cancel');
            };
        }]);